package com.wanshi.controller;


import com.wanshi.client.OrdersClient;
import com.wanshi.entity.Orders;
import com.wanshi.entity.User;
import com.wanshi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-21
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private OrdersClient ordersClient;
        //1 查询所有用户
        @GetMapping("/list")
        public List<User> list(){
            return userService.list();
        }

        //2 根据id查询用户
    @GetMapping("/{id}")
    public User findById(@PathVariable String id){
        return userService.getById(id);
          }

        //3 新增用户
        @PostMapping
        public String addUser(@RequestBody User user){
            userService.save(user);
            return "添加成功";
        }

        //4 根据id查询用户及对应的订单
        @GetMapping("/orders/{id}")
        public User findUserAddOrdersById(@PathVariable String id){
            User user =  userService.getById(id);
            List<Orders> orders = ordersClient.findByUserId(id);
            user.setOrders(orders);
            return user;
        }
}

